'use strict';

export default class {
	/*
	 * 格式化主题数据为选项。
	 * @param datas 数据
	 * @param key 筛选关键字
	 * @param value 筛选值
	 * @return 选项
	 */
	static toOptions(datas = '[{}]', key, value) {
		datas = JSON.parse(datas);
		let options = [];
		let getSubOptions = subTheme => {
			let subThemes = subTheme.split(',');
			let subOptions = subThemes.map((x, j) => {
				return {
					value: x,
					label: x,
				};
			});

			return subOptions;
		}

		if (key !== undefined) {
			for (let i = 0; i < datas.length; i++) {
				if (datas[i][key] === value) {
					options.push({
						value: datas[i].themeName,
						label: datas[i].themeName,
						children: getSubOptions(datas[i].subTheme),
					});
				}
			}
		} else {
			options = datas.map((x, i) => {
				return {
					value: x.themeName,
					label: x.themeName,
					children: getSubOptions(x.subTheme),
				};
			});
		}

		return options;
	}
}
